package com.sdnu.iosclub.qvs.config;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @author Konjacer
 * @create 2022-09-05 11:55
 */
@Configuration
@MapperScan("com.sdnu.iosclub.qvs.mapper")
@EnableTransactionManagement //支持事务
public class QvsConfig implements TransactionManagementConfigurer {

    @Resource(name = "tranManager")
    private DataSourceTransactionManager tranManager;
    /**
     * 逻辑删除插件
     */
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    @Bean("tranManager")
    public DataSourceTransactionManager tranManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    //这里表明默认使用tranManager这个事务管理器
    @Override
    public TransactionManager annotationDrivenTransactionManager() {
        return tranManager;
    }
}
